package com.lzl.educenter.controller;


import com.lzl.common_utils.JwtUtils;
import com.lzl.common_utils.R;
import com.lzl.common_utils.ordervo.UcenterMemberOrder;
import com.lzl.educenter.entity.UcenterMember;
import com.lzl.educenter.entity.vo.RegisterVo;
import com.lzl.educenter.service.UcenterMemberService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2022-04-06
 */
@RestController
@RequestMapping("/educenter/member")
//@CrossOrigin
public class UcenterMemberController {

    @Autowired
    private UcenterMemberService memberService;

    //登录
    @PostMapping("login")
    public R loginUser(@RequestBody UcenterMember member){
        //调用service方法实现登录
        //登录方法需要返回token，--使用jst生成---单点登录的实现
        String token = memberService.login(member);

        return R.ok().data("token",token);
    }

    //注册
    @PostMapping("register")
    public R registerUser(@RequestBody RegisterVo registerVo){

        memberService.register(registerVo);

        return R.ok();
    }

    /**
     * 根据token获取用户信息
     * @param request   工具类jwtutils需要传request-根据token字符串获取会员id
     * @return
     */
    @GetMapping("getMemberInfo")
    public R getMemberInfo(HttpServletRequest request){
        //调用jwt工具类的方法，根据request对象获取头信息，返回用户id
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        //查询数据库根据用户id获取用户信息
        UcenterMember member = memberService.getById(memberId);
        return R.ok().data("userInfo",member);

    }


    //被远程调用--根据用户id获取用户信息
    //现在返回值 不能是 R对象--当然R对象也可以。但是取值不方便
    //返回的对象--在调用端（对象是UcenterMember）和在被调用端需要一样（创一个一模一样的对象在工具类中）-
    //不能是两个不同的对象--里面的属性一样
    @PostMapping("getUserInfoOrder/{id}")
    public UcenterMemberOrder getUserInfoOrder(@PathVariable String id){

        UcenterMember member = memberService.getById(id);
        //把member对象里面的值复制给UcenterMemnerOrder对象
        UcenterMemberOrder ucenterMemberOrder = new UcenterMemberOrder();
        BeanUtils.copyProperties(member,ucenterMemberOrder);
        return ucenterMemberOrder;
    }

    //被service-statistics远程调用-查询某一天注册人数
    @GetMapping("countRegister/{day}")
    public R countRegister(@PathVariable("day") String day){

        Integer count = memberService.countRegisterDay(day);

        return R.ok().data("count",count);

    }





}

